package com.qf.Service.impl;

import com.qf.Dao.UserDao;
import com.qf.Dao.impl.UserDaoImpl;
import com.qf.Service.UserService;
import com.qf.Utils.PageUtils;
import com.qf.entity.SysUser;
import com.qf.entity.UserManagEmentPaginationDisplay;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    /***
     * 4位数的验证码
     * @return
     */
//    @Override
//    public String checkCode() {
//        return VerifyCodeUtils.getGenerateVerifyCode(4);
//    }
    @Override
    public PageUtils<SysUser> selectByPage(String currentPageNoStr, String pageSizeStr) {
        Integer currentPageNo = 1;
        //对当前页进行处理
        if (currentPageNoStr == null || "".equals(currentPageNoStr)) {
            currentPageNo = 1;
        } else {
            currentPageNo = Integer.parseInt(currentPageNoStr);
        }

        Integer pageSize = 2;

        //对页量进行处理
        if (pageSizeStr == null || "".equals(pageSizeStr)) {
            pageSize = 3;
        } else {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        PageUtils pageUtils = null;

        try {
            //查询总记录数
            Integer totalPageCount = userDao.selectCount();
            //根据总记录数计算出总页数
            Integer totalPageSize = totalPageCount % pageSize == 0 ? totalPageCount / pageSize : totalPageCount / pageSize + 1;
            //查询的数据
            List<UserManagEmentPaginationDisplay> userList = userDao.selectByUser(currentPageNo, pageSize);
            pageUtils = new PageUtils();
            pageUtils.setCurrentPageNo(currentPageNo);
            pageUtils.setPageSize(pageSize);
            pageUtils.setTotalPageCount(totalPageCount);
            pageUtils.setTotalPageSize(totalPageSize);
            pageUtils.setList(userList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pageUtils;
    }

    @Override
    public int addUser(SysUser u) {
        try {
            return userDao.addUser(u);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public SysUser idByUser(int id) {
        //查询的数据
        try {
            List<SysUser> userList = userDao.idByUser(id);
//            SysUser sysUser=new SysUser();
//                account,password,name,sex,deptid,email,phone,`status`,createtime
//            sysUser.setAccount(String.valueOf(userList.get(0)));
//            sysUser.setPassword(String.valueOf(userList.get(1)));
//            sysUser.setName(String.valueOf(userList.get(2)));
//            sysUser.setSex(Long.parseLong(userList.get(3).toString()));
//            sysUser.setDeptid(Long.parseLong(String.valueOf(userList.get(4))));
//            sysUser.setEmail(String.valueOf(userList.get(5)));
//            sysUser.setPhone(String.valueOf(userList.get(6)));
//            sysUser.setStatus(Long.parseLong(String.valueOf(userList.get(7))));
//            sysUser.setCreatetime(userList.get(8));
//            System.out.println(sysUser);

            //调用修改
            return userList.get(0);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int modify(SysUser sysUser) {
        try {
            return userDao.modify(sysUser);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int delUser(int id) {
        try {
            return userDao.delUser(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String resetPassword(int id) {
        try {
            return userDao.resetPassword(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int freeze(int id) {
        try {
            return userDao.freeze(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int unfreeze(int id) {
        try {
            return userDao.unfreeze(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int roleAssignment(int id, String roleid) {
        try {
            if (roleid.equals("医生")) {
                return userDao.roleAssignment(id, 5);
            } else if (roleid.equals("病人")) {
                return userDao.roleAssignment(id, 6);
            } else {
                return userDao.roleAssignment(id, 1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<UserManagEmentPaginationDisplay> ConditionalQuery(String str, String startTime, String endTime, Integer currentPageNo, Integer pageSize) {
        try {
            return userDao.ConditionalQuery(str, startTime, endTime, currentPageNo, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
